//
//  HHSignInViewController.m
//  WeCommunity
//
//  Created by HappyMilk 01 on 2017/8/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "HHSignInViewController.h"
#define SIGN_IN_KEY @"userSignInDateKey"



@interface HHSignInViewController ()
@property (weak, nonatomic) IBOutlet UIButton *signBtn;

@property (weak, nonatomic) IBOutlet UILabel *signLabel;
@property (nonatomic, copy)NSArray<NSString *> *signInDates;//签到日期 yyyyMMdd格式的字符串
@end

@implementation HHSignInViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initViews];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self refreshViews];
}

-(NSArray<NSString *> *)signInDates{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    return [userDefault arrayForKey:SIGN_IN_KEY];
}

-(void)initViews{
    self.title = @"签到赢积分";

    self.view.backgroundColor = HH_COLOR_RGB(246, 246, 246);
}

-(void)refreshViews{
    if([self isSignInToday]){
        [_signBtn setTitle:@"今日已签到" forState:0];
    }else{
        [_signBtn setTitle:@"签到" forState:0];
    }
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    _signLabel.text = [NSString stringWithFormat:@"已经连续签到%lu天",[userDefault arrayForKey:SIGN_IN_KEY].count];
    
}

-(BOOL)isSignInToday{
    for(NSString *dateStr in self.signInDates){
        if([dateStr isEqualToString:[self dateStrForToday]]){
            return YES;
        }
    }
    
    return NO;
}


- (IBAction)onSignBtn:(id)sender {
    if([self isSignInToday]){
        [self sureAlertWithTitle:nil andMesg:@"今日已签到，请明天再来哦!"];
    }else{
        [self signIn];
    }
}

//签到
-(void)signIn{
    NSMutableArray *signDates = [[NSMutableArray alloc]initWithArray:self.signInDates];
    _signBtn.enabled = false;
  
    NSString *lastSignDateStr = [signDates lastObject];
    
    //已经签到7天 或 没有连续签到
    if(signDates.count >=7 || [lastSignDateStr isEqualToString:[self dateStrForLastDay]] == NO){
        [signDates removeAllObjects];
    }
    [signDates addObject:[self dateStrForToday]];
    
    NSInteger coins = 10 + (signDates.count -1)*5;
    [[UserInfoManger shared]addCoins:@(coins) complete:^(BOOL success, NSString *error) {
        _signBtn.enabled = true;
        
        if(success){
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:signDates forKey:SIGN_IN_KEY];
            [self refreshViews];
            
            [self sureAlertWithTitle:nil andMesg:[NSString stringWithFormat:@"今日已签到赠送积分 +%ld",(long)coins]];
        }
    }];
    
}



-(NSString *)dateStrForToday{
    return [self dateStrForDate:[NSDate date]];
}

//前一天
-(NSString *)dateStrForLastDay{
    NSDate *lastDate = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:[NSDate date]];
    
    return [self dateStrForDate:lastDate];
}


// yyyyMMdd 格式字符串
-(NSString *)dateStrForDate:(NSDate *)date{
   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 
    [dateFormatter setDateFormat:@"yyyyMMdd"];

    return [dateFormatter stringFromDate:date];
}

@end
